package com.example.myys.util

import android.content.res.AssetManager
import com.example.myys.MyApplication
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader

/**
 * 文件帮助类
 */
object FileUtil {
    //从assts资源文件中读取内容转成json字符串
    fun fileToJsonStr(fileName: String): String? {
        val stringBuilder = StringBuilder()
        try {
            val assetManager: AssetManager = MyApplication.instance.assets

            val isr = InputStreamReader(assetManager.open(fileName))
            val bf = BufferedReader(isr)
            var line: String?
            while (bf.readLine().also { line = it } != null) {
                stringBuilder.append(line)
            }
            bf.close()
            isr.close()
            return stringBuilder.toString()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return null
    }

}